function RessourceManager()
{
	RessourceManager.nelements = 0;
	this.allLoaded = null;	
}

RessourceManager.prototype.addImage = function(filename)
{
	RessourceManager.nelements += 1;
	var image = new Image();
	image.src= filename;
	image.ref = this;

	image.onload = function()
		{
			if((--RessourceManager.nelements) == 0)
			{
				this.ref.allLoaded();
			}
		}

	var ressource = new Object();
	ressource.image = image;

	return ressource;
}

RessourceManager.prototype.restore2D = function(matrix)
{
	mat4.identity(matrix);
	var uv = gl.getUniformLocation(shaderProgram, "vMatrix");
	gl.uniformMatrix4fv(uv, false, matrix);
}

RessourceManager.prototype.show = function(ressource)
{
	{
		gl.activeTexture(gl.TEXTURE0);
		gl.uniform1i(gl.getUniformLocation(shaderProgram, "imgdata"), 0);

		gl.bindBuffer(gl.ARRAY_BUFFER, ressource.uv);
		gl.vertexAttribPointer(aTextureLocation, 2, gl.FLOAT, false, 0, 0);

		gl.bindBuffer(gl.ARRAY_BUFFER, ressource.vertices);
		gl.vertexAttribPointer(aLocation, 3, gl.FLOAT, false, 0, 0);
	}

	gl.bindTexture(gl.TEXTURE_2D, ressource.texture);
	gl.drawArrays(gl.TRIANGLES, 0, 6);
    //gl.drawElements(gl.TRIANGLES, 4, gl.UNSIGNED_SHORT, 0);
}

RessourceManager.prototype.createTexture = function(ressource)
{
	ressource.texture = gl.createTexture();

	gl.bindTexture(gl.TEXTURE_2D, ressource.texture);
	gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, ressource.image);
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
	gl.generateMipmap(gl.TEXTURE_2D);
	gl.bindTexture(gl.TEXTURE_2D, null);
}

RessourceManager.prototype.createPlane = function(ressource)
{
	var vertices = [
		-1.0, -1.0,  -0.9,
		1.0, -1.0,  -0.9,
		1.0,  1.0,  -0.9,

		-1.0,  -1.0,  -0.9,
		-1.0,  1.0,  -0.9,
		1.0, 1.0,  -0.9
	];

	var uv = [
		0.0, 0.0,
		1.0, 0.0,
		1.0, 1.0,

		0.0, 0.0,
		0.0, 1.0,
		1.0, 1.0
	];

	ressource.vertices = gl.createBuffer();
	ressource.uv = gl.createBuffer();

	gl.bindBuffer(gl.ARRAY_BUFFER, ressource.vertices);
	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

	gl.bindBuffer(gl.ARRAY_BUFFER, ressource.uv);
	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(uv), gl.STATIC_DRAW);
}
